//
//  MJDIYFooter.m
//  yuan
//
//  Created by apple on 16/1/26.
//  Copyright © 2016年 MDJ. All rights reserved.
//

#import "MJDIYFooter.h"

@interface MJDIYFooter()
{
    __unsafe_unretained UIImageView *_arrowView;
}
@property (weak, nonatomic) UIActivityIndicatorView *loadingView;
@property (weak, nonatomic) UIImageView *loadView;
@end

@implementation MJDIYFooter
#pragma mark - 懒加载子控件
- (UIImageView *)arrowView
{
    if (!_arrowView) {
        UIImage *image = [UIImage imageNamed:MJRefreshSrcName(@"arrow.png")] ?: [UIImage imageNamed:MJRefreshFrameworkSrcName(@"arrow.png")];
        UIImageView *arrowView = [[UIImageView alloc] initWithImage:image];
        [self addSubview:_arrowView = arrowView];
    }
    return _arrowView;
}

-(UIImageView *)loadView{
    if (!_loadView) {
        UIImage *images = [UIImage imageNamed:MJRefreshSrcName(@"bg.png")] ?: [UIImage imageNamed:MJRefreshFrameworkSrcName(@"bg.png")];
        UIImageView *arrowViews = [[UIImageView alloc] initWithImage:images];
        
        [self addSubview:_loadView = arrowViews];
    }
    return _loadView;
}

- (UIActivityIndicatorView *)loadingView
{
    if (!_loadingView) {
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:self.activityIndicatorViewStyle];
        loadingView.hidesWhenStopped = YES;
        [self addSubview:_loadingView = loadingView];
    }
    return _loadingView;
}

- (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle
{
    _activityIndicatorViewStyle = activityIndicatorViewStyle;
    
    self.loadingView = nil;
    [self setNeedsLayout];
}
#pragma makr - 重写父类的方法
- (void)prepare
{
    [super prepare];
    
    self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
}

- (void)placeSubviews
{
    [super placeSubviews];
    
    self.stateLabel.frame = CGRectMake(0,0,120,21);
    self.stateLabel.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5);
    self.stateLabel.textColor = [UIColor colorWithHexString:@"#06ab9e"];
    // 箭头的中心点
    
    [self.arrowView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.centerY).with.offset(0);
        make.right.equalTo(self.stateLabel.left).with.offset(0);
        make.width.mas_equalTo(self.arrowView.image.size.width);
        make.height.mas_equalTo(self.arrowView.image.size.height);
    }];
    // 圈圈
    if (self.loadView.constraints.count == 0) {
        [self.loadView makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.centerY).with.offset(0);
            make.right.equalTo(self.stateLabel.left).with.offset(0);
            make.width.mas_equalTo(self.loadView.image.size.width);
            make.height.mas_equalTo(self.loadView.image.size.height);
        }];
    }

    
//    CGFloat arrowCenterX = self.mj_w * 0.56;
//    if (!self.stateLabel.hidden) {
//        arrowCenterX -= 100;
//    }
//    CGFloat arrowCenterY = self.mj_h * 0.5;
//    CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);
//    
//    // 箭头
//    if (self.arrowView.constraints.count == 0) {
//        self.arrowView.mj_size = self.arrowView.image.size;
//        self.arrowView.center = arrowCenter;
//    }
//    
//    // 圈圈
//    if (self.loadView.constraints.count == 0) {
//        self.loadView.mj_size = self.loadView.image.size;
//        self.loadView.center = self.arrowView.center;
//    }
    self.loadView.alpha = 0.0;
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    
    // 根据状态做事情
    if (state == MJRefreshStateIdle) {
        if (oldState == MJRefreshStateRefreshing) {
            self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
                self.loadView.alpha = 0.0;
            } completion:^(BOOL finished) {
                if (self.state != MJRefreshStateIdle){
                    return;
                }else{
                    [self.loadView.layer removeAllAnimations];
                }
                self.arrowView.hidden = NO;
            }];
        } else {
            self.arrowView.hidden = NO;
            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
            }];
        }
    } else if (state == MJRefreshStatePulling) {
        self.arrowView.hidden = NO;
        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
            self.arrowView.transform = CGAffineTransformIdentity;
        }];
    } else if (state == MJRefreshStateRefreshing) {
        self.arrowView.hidden = YES;
        self.loadView.alpha = 1.0;
        self.stateLabel.text = @"加载中...";
        [self loadViewSport];
    } else if (state == MJRefreshStateNoMoreData) {
        self.arrowView.hidden = YES;
        self.loadView.hidden = YES;
        self.stateLabel.text = @"到底了";
    }
}

-(void)loadViewSport{
    
    CABasicAnimation * rotationAnimations = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimations.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimations.duration = 0.5;
    rotationAnimations.cumulative = YES;
    rotationAnimations.repeatCount = FLT_MAX;
    
    [self.loadView.layer addAnimation:rotationAnimations forKey:@"rotationAnimation"];
}


@end
